﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using CubicFWNet.Mvc.Models;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class SupplierController : ControllerBase
    {
        private CustomerApp _customerApp;
        private CustomerProductApp _customerProductApp;

        public SupplierController()
        {
            _customerApp = new CustomerApp();
            _customerProductApp = new CustomerProductApp();
        }

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 供应商列表
        /// </summary>
        /// <returns></returns>
        public ActionResult SupplierList(int page = 1, string name = "")
        {
            ViewBag.Name = name;
            Guid salerID = operatorModel.UserId;
            if (operatorModel.UserName == "admin")
            {
                salerID = new Guid();
            }
            IPagedList<Customer> cusList = _customerApp.GetList(name, salerID, 2).ToPagedList(page, 10);
            return View(cusList);
        }

        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <returns></returns>
        public ActionResult SupplierAdd()
        {
            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 添加供应商[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SupplierAdd(FormCollection fc)
        {
            Customer cus = new Customer();
            cus.Id = Guid.NewGuid();
            cus.Name = fc["Name"];
            cus.ShortName = fc["ShortName"];

            cus.UserId = operatorModel.UserId;
            cus.UserName = operatorModel.FamilyName;

            cus.Address = fc["Address"];
            cus.Corporation = fc["Corporation"];
            cus.Product = fc["Products"];
            cus.Yield = Convert.ToDecimal(fc["Yield"]);
            cus.Range = fc["Range"];
            cus.AccountPeriod = fc["AccountPeriod"];
            cus.BillingFlow = fc["BillingFlow"];
            cus.StatementDate = fc["StatementDate"];
            cus.StatementCount = fc["StatementCount"];
            cus.Memo = fc["Memo"];
            cus.CreateTime = DateTime.Now;
            cus.CreatorId = operatorModel.UserId;
            cus.CreatorName = operatorModel.FamilyName;
            cus.Principal = fc["Data"];
            cus.Type = 2;
            cus.Market = 1;
            cus.Status = 1;

            int i = _customerApp.Insert(cus);
            if (i == 1)
            {
                return RedirectToAction("SupplierList", "Supplier");
            }
            else
            {
                ViewBag.Info = "抱歉，操作失败！";
            }

            return View();
        }

        /// <summary>
        /// 编辑供应商
        /// </summary>
        /// <returns></returns>
        public ActionResult SupplierEdit()
        {
            ViewBag.RealName = operatorModel.FamilyName;
            string cid = Request.QueryString["CID"];
            Guid id = new Guid(cid);
            Customer cus = _customerApp.GetInfo(id);
            if (cus != null)
            {
                ViewBag.Customer = cus;
            }
            return View();
        }

        /// <summary>
        /// 编辑供应商[提交数据]
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SupplierEdit(FormCollection fc)
        {
            string cid = fc["CustomerID"];
            Guid id = new Guid(cid);
            Customer cus = _customerApp.GetInfo(id);
            if (null != cus)
            {
                cus.Name = fc["Name"];
                cus.ShortName = fc["ShortName"];
                //cus.UserId = new Guid(fc["Saler"]);
                cus.Address = fc["Address"];
                cus.Corporation = fc["Corporation"];
                cus.Product = fc["Products"];
                cus.Yield = Convert.ToDecimal(fc["Yield"]);
                cus.Range = fc["Range"];
                cus.AccountPeriod = fc["AccountPeriod"];
                cus.BillingFlow = fc["BillingFlow"];
                cus.StatementDate = fc["StatementDate"];
                cus.StatementCount = fc["StatementCount"];
                cus.Memo = fc["Memo"];
                cus.Status = Convert.ToInt32(fc["Status"]);
                cus.Principal = fc["Data"];

                int i = _customerApp.Update(cus);
                if (i == 1)
                {
                    return RedirectToAction("SupplierList", "Supplier");
                }
                else
                {
                    ViewBag.Info = "抱歉，操作失败！";
                }
            }

            return View();
        }

    }
}